home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Programming / Programming Tools / Turbo Pascal / Logweed / LOG31.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1987-07-01  |  16.9 KB  |  442 lines  |  [TEXT/ttxt]

  1. Program Log;
  2. (****************************************************************************)
  3. (*                                                                          *)
  4. (*                               LOG                                        *)
  5. (*                           Revision 3.1                                   *)
  6. (*             Written by Don Gancsos and Robert Wilcox                     *)
  7. (*                                                                          *)
  8. (*             A program which logs computer operation and calculates       *)
  9. (*             the elapsed time between log entries.                        *)
  10. (*             Logs date, time, elapsed time and comment to the             *)
  11. (*             file "TIME.LOG"                                              *)
  12. (*                                                                          *)
  13. (*                  Use: LOG comment                                        *)
  14. (*                                                                          *)
  15. (*             Modified by R. Wilcox 3-14-86                                *)
  16. (*                - Now puts log file in root directory of drive A          *)
  17. (*                  rather than in the logged-in directory.                 *)
  18. (*                - Uses command line (up to 50 characters) as comment.     *)
  19. (*                - Log file changed to 'TIME.LOG' (3-23-86)                *)
  20. (*             Rev 2.0 modifications 4-9-86                                 *)
  21. (*                - Uses random file allowing a "SHOW" program to           *)
  22. (*                  display start and end log entries with the              *)
  23. (*                  calculated elapsed time.                                *)
  24. (*                - Log file changed to 'NEWTIME.LOG'.                      *)
  25. (*                                                                          *)
  26. (*             Rev 3.0 modifications 5-10-87 by Rick Johnson                *)
  27. (*                                                                          *)
  28. (*                  Use: LOG <off|[comment] /a|/*>                          *)
  29. (*                                                                          *)
  30. (*                - Keeps log in random file 'TIME.LOG' in path             *)
  31. (*                  C:\UTILITY.                                             *)
  32. (*                - Random file records now store:                          *)
  33. (*                  1) Time category, e.g. 'B' for business.                *)
  34. (*                  2) Start date.                                          *)
  35. (*                  3) Start time.                                          *)
  36. (*                  4) End time.                                            *)
  37. (*                  5) Elapsed time (string).                               *)
  38. (*                  6) Elapsed time (real no.).                             *)
  39. (*                  8) Comment (up to 12 characters).                       *)
  40. (*                - Session terminated and elapsed time calculated          *)
  41. (*                  with 'LOG OFF' command.                                 *)
  42. (*                - Time category invoked with '/a' parameter where         *)
  43. (*                  'a' is any alpha character a through z.                 *)
  44. (*                - Time categories 'B' and 'P' noted as 'Business'         *)
  45. (*                  or 'Personal' use in program's screen messages          *)
  46. (*                - Times displayed and totaled with '/*' parameter         *)
  47. (*                  and optionally written to disk file:                    *)
  48. (*                  1) Text format for word processor or                    *)
  49. (*                  2) Comma-delimited for spreadsheet.                     *)
  50. (*                  SHOW.COM no longer needed.                              *)
  51. (*                                                                          *)
  52. (*             Rev 3.1 modifications 5-18-87 by Rick Johnson                *)
  53. (*                - Random file records no longer store elapsed             *)
  54. (*                  time as real number--calculation done at summary        *)
  55. (*                - Utilities included to convert 2.0 or 3.0 fies to 3.1    *)
  56. (*                - Utility included to strip out entries of less than      *)
  57. (*                  a specified time.                                       *)
  58. (*                - Summary calculates category percentages of total        *)
  59. (*                  PRODUCTIVE time, ignoring administrative time.          *)
  60. (*                                                                          *)
  61. (****************************************************************************)
  62.  
  63.  
  64. Type
  65.   Datestring = string[8];
  66.   CommandString  = string[50];
  67.   CommentString = string[12];
  68.   Filename = string[24];
  69.   Registers    = record
  70.                 AX,BX,CX,DX,BP,SI,DI,DS,ES,Flags: Integer;
  71.             End;
  72.  
  73. LogRec = Record
  74.   usage: char;              {usage type, i.e. business/personal}
  75.   start_date_log : Datestring;     {start date}
  76.   start_time_log : Datestring;     {start time}
  77.   elapsed_log    : Datestring;     {elapsed time}
  78.   comment        : CommentString;  {comment}
  79.   end;
  80.  
  81. Var
  82.   Mon, Day, Year : string[2];
  83.   logfile        : file of LogRec;
  84.   realtime       : Real;
  85.   end_time       : datestring;
  86.   Buffer         : CommandString;
  87.   CL             : CommandString absolute cseg:$80;
  88.   log_data       : logrec;
  89.   logoff         : boolean;
  90.  
  91. Const
  92.   logfilename    : filename = 'C:\UTILITY\TIME.LOG';
  93.  
  94. Function Date: Datestring;
  95. var
  96.   RSet   : Registers;
  97.   tempstring: Datestring;
  98.   i: Integer;
  99. Begin
  100.   With Rset do
  101.   Begin
  102.     AX   :=  $2A00;        { DOS get date function }
  103.     MSDos(RSet);
  104.     CX := cx mod 100;
  105.     str(CX:2,year);
  106.     str(Hi(DX):2,mon);
  107.     str(Lo(DX):2,day);
  108.   End;
  109.   tempstring := mon + '/' + day + '/' + year;
  110.    for i := 1 to 8 do
  111.      if tempstring[i] = ' ' then
  112.        tempstring[i] := '0';
  113. Date := tempstring;
  114. End;
  115.  
  116. Function Time: Datestring;
  117. var
  118.   RSet                 : registers;
  119.   hour, minute, second : string[2];
  120.   i                    : integer;
  121.   tempstring           : Datestring;
  122. Begin
  123.    rset.ax := $2C * 256;
  124.    MsDos(rset);
  125.    str((rset.cx div 256):2,hour);
  126.    str((rset.cx mod 256):2,minute);
  127.    str((rset.dx div 256):2,second);
  128.    tempstring := hour + ':' + minute + ':' + second;
  129.    for i := 1 to 8 do
  130.      if tempstring[i] = ' ' then
  131.        tempstring[i] := '0';
  132.    Time := tempstring;
  133. End;  {Time}
  134.  
  135. Function SecToString(secs: integer): Datestring;
  136. var
  137.   t: integer;
  138.   result: Datestring;
  139.   tempresult: Datestring;
  140. Begin
  141.   Str(secs div 3600:2,tempresult);
  142.   result := tempresult + ':';
  143.   t:= secs mod 3600;
  144.   Str(t div 60:2,tempresult);
  145.   result := result +tempresult + ':';
  146.   Str(t mod 60:2,tempresult);
  147.   result := result + tempresult;
  148.   For t := 1 to 8 do if result[t] = ' ' then result[t] := '0';
  149.   SecToString := result;
  150. end;
  151.  
  152. Function elapsed_time(start_str, end_str: Datestring): Datestring;
  153. var
  154.   i,j,k,starttime,endtime: integer;
  155. begin
  156.   val(copy(end_str,1,2),i,j);
  157.   endtime := i * 3600;
  158.   val(copy(end_str,4,2),i,j);
  159.   endtime := endtime + (i * 60);
  160.   val(copy(end_str,7,2),i,j);
  161.   endtime := endtime + i;
  162.   val(copy(start_str,1,2),i,j);
  163.   starttime := i * 3600;
  164.   val(copy(start_str,4,2),i,j);
  165.   starttime := starttime + (i * 60);
  166.   val(copy(start_str,7,2),i,j);
  167.   starttime := starttime + i;
  168.   k := endtime - starttime;
  169.   elapsed_time := SecToString(k);
  170. end;
  171.  
  172. Function elapsed_real_time(tempstring: Datestring): real;
  173. var
  174.   temp, temptime : real;
  175.   err            : integer;
  176. begin
  177.   val(copy(tempstring,1,2),temptime,err); {get hours}
  178.   val(copy(tempstring,4,2),temp,err);
  179.   temptime := temptime + (temp/60);       {get fraction of hours from minutes}
  180.   val(copy(tempstring,7,2),temp,err);
  181.   temptime := temptime + (temp/3600);     {get fraction of hours from seconds}
  182.   elapsed_real_time := temptime;
  183. end;
  184.  
  185. Procedure initialize_variables;
  186. begin
  187.   logoff:=false;
  188.   log_data.usage := '@';
  189.   log_data.start_date_log:= Date;
  190.   log_data.start_time_log:= Time;
  191.   log_data.elapsed_log:= '00:00:00';
  192.   log_data.comment:= '';
  193.   end_time:=Time;
  194. end;
  195.  
  196. Function uppercase(tempstring: CommandString; startpos, number: integer): CommandString;
  197. var temp : CommandString;
  198.     i    : integer;
  199. begin
  200.   if length(tempstring) >= startpos then
  201.   begin
  202.     tempstring:=copy(tempstring,startpos,number);
  203.     for i := 1 to length(tempstring) do
  204.       tempstring[i] := upcase(tempstring[i]);
  205.   end
  206.   else tempstring:='';
  207.   uppercase:=tempstring;
  208. end; {procedure uppercase}
  209.  
  210. procedure summarize;
  211. var
  212.     total_time       : real;
  213.     category_total   : array [64..90] of real;
  214.     i, counter       : integer;
  215.     ch               : char;
  216.     disk_file,
  217.     delimited        : boolean;
  218.     textfile         : text;
  219. const
  220.     textfilename     : filename = 'C:\UTILITY\TIME.TXT';
  221. begin
  222.   for i:=ord('A') to ord('Z') do
  223.     category_total[i]:=0;
  224.   total_time:=0;
  225.   lowvideo; writeln; writeln;
  226.   writeln('LOG''s summary function requested.  This function will list the contents');
  227.   writeln('of the TIME.LOG file to the screen.  You may also have a copy written');
  228.   writeln('to a disk file named TIME.TXT, which can be viewed by a word processor');
  229.   writeln('or read into a spreadsheet.'); writeln; writeln;
  230.   write('Would you like a disk file record of your logon/logoff file? (Y/N) ');
  231.   read(kbd,ch); writeln(upcase(ch));
  232.   writeln;
  233.   if upcase(ch)='Y' then disk_file:=true else disk_file:=false;
  234.   if disk_file then
  235.   begin {get file specs}
  236.     write('Would you like the file comma-delimited for spreadsheet use? (Y/N) ');
  237.     read(kbd,ch); writeln(upcase(ch));
  238.     if upcase(ch)='Y' then delimited:=true else delimited:=false;
  239.     writeln;
  240.     write('Would you like the file written to ',textfilename,'? (Y/N) ');
  241.     read(kbd,ch); writeln(upcase(ch));
  242.     if upcase(ch)='N' then
  243.     begin  {get new output filename}
  244.       writeln;
  245.       write('New drive/path/filename (max. 24 characters): ________________________');
  246.       gotoXY(47,whereY);
  247.       readln(textfilename);
  248.     end;
  249.     Assign(textfile,textfilename);
  250.     rewrite(textfile);
  251.     if delimited then
  252.     begin
  253.       write(textfile,'"USAGE","DATE","START TIME",');
  254.       writeln(textfile,'"RECORDED TIME","CALCULATED TIME","COMMENT"');
  255.     end
  256.     else
  257.     begin
  258.       writeln;
  259.       writeln(textfile,'     DATE     START    ELAPSED     COMMENT');
  260.     end;
  261.   end;  {get file specs}
  262.   counter:=22;
  263.   for i:=0 to filesize(logfile)-1 do
  264.   begin {read file loop}
  265.     counter:=counter+1;
  266.     if counter=23 then
  267.     begin
  268.       write('Press any key to continue... ');
  269.       read(kbd,ch); writeln; writeln;
  270.       writeln('     DATE     START    ELAPSED    COMMENT');
  271.       counter:=0;
  272.     end;
  273.     seek(logfile,i);
  274.     read(logfile,log_data);
  275.     with log_data do
  276.       begin
  277.         write(usage,'  ',start_date_log,'  ',start_time_log,'  ');
  278.         writeln(elapsed_log,'  ',comment);
  279.         if disk_file then
  280.         begin
  281.           if delimited then
  282.           begin
  283.             write(textfile,'"',usage,'","',start_date_log,'","',start_time_log,'","');
  284.             write(textfile,elapsed_log,'",');
  285.             writeln(textfile,elapsed_real_time(elapsed_log):6:2,',"',comment,'"');
  286.           end  {if delimited}
  287.         else
  288.           begin
  289.             write(textfile,usage,'  ',start_date_log,'  ',start_time_log,'  ');
  290.             write(textfile,elapsed_log,' ');
  291.             writeln(textfile,elapsed_real_time(elapsed_log):6:2,' hours, ',comment);
  292.           end;
  293.         end;  {if disk file}
  294.       end; {with log_data}
  295.     if not (log_data.usage in ['A'..'Z']) then
  296.       log_data.usage:='@';
  297.     category_total[ord(log_data.usage)]:=
  298.       category_total[ord(log_data.usage)]+
  299.       elapsed_real_time(log_data.elapsed_log);
  300.     total_time:=total_time+elapsed_real_time(log_data.elapsed_log);
  301.   end;  {read file loop}
  302.   close(logfile);
  303.   write('Press any key to continue... ');
  304.   read(kbd,ch); writeln; writeln;
  305.   writeln; writeln;
  306.   writeln('TOTAL LOGGED TIME:         ',total_time:6:2,' Hours');
  307.   writeln('TOTAL ADMINISTRATIVE TIME: ',category_total[64]:6:2,' Hours');
  308.   writeln('TOTAL PRODUCTIVE TIME:     ',total_time-category_total[64]:6:2,' Hours');
  309.   writeln;
  310.   if disk_file then
  311.   begin
  312.     if delimited then
  313.     begin
  314.       writeln(textfile); writeln(textfile);
  315.       writeln(textfile,'"TOTAL LOGGED TIME:         "',total_time:6:2,' Hours');
  316.       writeln(textfile,'"TOTAL ADMINISTRATIVE TIME: "',category_total[64]:6:2,' Hours');
  317.       writeln(textfile,'"TOTAL PRODUCTIVE TIME:     "',total_time-category_total[64]:6:2,' Hours');
  318.     end
  319.     else
  320.     begin
  321.       writeln(textfile); writeln(textfile);
  322.       writeln(textfile,'TOTAL LOGGED TIME:         ',total_time:6:2,' Hours');
  323.       writeln(textfile,'TOTAL ADMINISTRATIVE TIME: ',category_total[64]:6:2,' Hours');
  324.       writeln(textfile,'TOTAL PRODUCTIVE TIME:     ',total_time-category_total[64]:6:2,' Hours');
  325.       writeln(textfile);
  326.     end;
  327.   end; {if disk file}{header}
  328.   total_time:=total_time-category_total[64];
  329.   for i:= 65 to 90 do
  330.   begin  {total time by category}
  331.     if category_total[i]>0 then
  332.     begin
  333.       writeln(chr(i),': ',category_total[i]:6:2,' Hours (',
  334.         category_total[i]/total_time:4:2,'% of total)');
  335.       if disk_file and (not delimited) then writeln(textfile,chr(i),': ',category_total[i]:6:2,
  336.         ' Hours (',category_total[i]/total_time:4:2,'% of total)');
  337.       if disk_file and delimited then writeln(textfile,'"',chr(i),'",',category_total[i]:6:2,
  338.         category_total[i]/total_time:4:2);
  339.     end;
  340.   end; {total time by category}
  341.   if disk_file then close(textfile);
  342.   writeln;
  343.   halt;
  344. end; {subroutine}
  345.  
  346. procedure give_instructions;
  347. begin
  348.   writeln;
  349.   writeln('LOG Time tracking utility      Usage:');
  350.   writeln;
  351.   writeln('   "LOG APPLICATION /X /d"');
  352.   writeln('   to begin time tracking, where "APPLICATION"');
  353.   writeln('   is the name of your project (up to 12');
  354.   writeln('   characters) and the optional switch "X" can');
  355.   writeln('   be any character A through Z (LOG assumes');
  356.   writeln('   "B" is for business, "P" is for personal).');
  357.   writeln('   The optional switch "d" is any digit 1 through');
  358.   writeln('   4 representing the drive on which your data file');
  359.   writeln('   resides (1=A, 4=D, etc.).  LOG will look in the');
  360.   writeln('   logged directory of the specified drive, or if');
  361.   writeln('   no drive is specified, the subdirectory "UTILITY');
  362.   writeln('   on drive C.');
  363.   writeln;
  364.   writeln('   "LOG OFF"');
  365.   writeln('   to end time tracking on current project');
  366.   writeln;
  367.   writeln('   "LOG /*"');
  368.   writeln('   to view log file and optionally write it');
  369.   writeln('   to disk for use in an application program.');
  370.   writeln;
  371.   halt;
  372. end;
  373.  
  374. Begin   {program}
  375.  
  376.   lowvideo;
  377.   initialize_variables;
  378.   Buffer := CL;        {move command line into buffer}
  379.   if length(buffer)=0 then give_instructions;
  380.  
  381.   Assign(logfile,logfilename);
  382. {$I-}
  383.   Reset(logfile);
  384. {$I+}
  385.   If (ioresult = 1) then
  386.   begin
  387.     writeln('Can''t find ',logfilename,',--creating new file.');
  388.     delay(200);
  389.     rewrite(logfile);
  390.     write(logfile,log_data);
  391.   end;
  392.  
  393.     if uppercase(buffer,2,length(buffer))='OFF' then
  394.     begin
  395.       logoff:=true;
  396.       delete(buffer,1,4);
  397.     end; {if uppercase... }
  398.     while (pos('/',buffer)>0) and (length(buffer)>pos('/',buffer)) do
  399.     begin  {read switches}
  400.       if upcase(buffer[pos('/',buffer)+1]) in ['A'..'Z'] then
  401.         log_data.Usage:=upcase(buffer[pos('/',buffer)+1]);
  402.       if buffer[pos('/',buffer)+1] in ['1'..'4'] then
  403.         logfilename:=chr(ord(buffer[pos('/',buffer)+1])+16)+':TIME.LOG';
  404.       if buffer[pos('/',buffer)+1] = '*' then summarize;
  405.       Delete(buffer,pos('/',buffer),2);
  406.     end;
  407.     while (length(buffer)>0) and (buffer[1]=' ') do delete(buffer,1,1);
  408.     while (length(buffer)>0) and (buffer[length(buffer)]=' ') do delete(buffer,length(buffer),1);
  409.  
  410.   if logoff then
  411.   begin
  412.     seek(logfile,filesize(logfile)-1);
  413.     read(logfile,log_data);
  414.     with log_data do
  415.     begin
  416.       elapsed_log:= elapsed_time(start_time_log,end_time);
  417.     end; {with log_data}
  418.   end {if logoff}
  419.     else seek(logfile,filesize(logfile));
  420.   if length(buffer)>0 then log_data.comment:=copy(buffer,1,12);
  421.   Write(logfile,log_data);
  422.   Close(logfile);
  423.  
  424.   if logoff then
  425.   begin
  426.     writeln;
  427.     writeln;
  428.     write('Logged ',log_data.elapsed_log);
  429.     if length(log_data.comment)>0 then write(' toward ',log_data.comment);
  430.     if log_data.usage<>#0 then
  431.     begin
  432.       if log_data.usage='B' then write(' as Business time');
  433.       if log_data.usage='P' then write(' as Personal time');
  434.       if log_data.usage='@' then write(' as Misc/Administrative time');
  435.       if not (log_data.usage in ['B','P','@']) then
  436.         write(' as "',log_data.usage,'" time');
  437.     end; {if usage <>#0}
  438.     writeln('.');
  439.     delay(2000);
  440.   end; {if logoff}
  441. End.
  442.